package com.mtt.erp.freight.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.mtt.erp.common.annotation.Log;
import com.mtt.erp.common.api.R;
import com.mtt.erp.common.api.ResultCode;
import com.mtt.erp.common.domain.QueryRequest;
import com.mtt.erp.freight.domain.McaFile;
import com.mtt.erp.freight.dto.McaCreateDto;
import com.mtt.erp.freight.dto.McaDto;
import com.mtt.erp.freight.service.IMcaFileService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import java.util.List;

/**
 * @author MrBird
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/express")
@Api(value = "快递信息",tags = "M006-物流-快递信息相关接口")
public class McaFileController{

    @Autowired
    private IMcaFileService iMcaFileService;

    @Log("查询快递公司信息")
    @GetMapping
    //@RequiresPermissions("express:view")
    @ApiOperation(value = "查询快递信息",notes = "分页查询：查询快递信息")
    public R<IPage<McaFile>> mcaPage(QueryRequest request, McaDto mcaFile) {
        IPage<McaFile> mcaFileIPage = iMcaFileService.findMca(request, mcaFile);
        if(mcaFileIPage.getTotal()>0){
            return R.data(mcaFileIPage);
        }else{
            return R.fail(ResultCode.NO_CONTENT);
        }
    }
    @Log("查询快递公司信息")
    @GetMapping("/list")
    @ApiOperation(value = "查询快递信息",notes = "查询快递信息列表")
    public R<List<McaFile>> mcdList() {
        List<McaFile> mcaList = iMcaFileService.findMcaList();
        if (mcaList!=null && mcaList.size() > 0){
            return R.data(mcaList);
        }else{
            return R.fail(ResultCode.NO_CONTENT);
        }
    }

    @Log("添加快递信息")
    @PostMapping
    //@RequiresPermissions("express:add")
    @ApiOperation(value = "添加快递信息",notes = "添加快递信息,传入mcaFile信息")
    public R addMca(@Valid McaCreateDto mcaCreateDto){
        try {
            Boolean addMca = this.iMcaFileService.createMca(mcaCreateDto);
            if(addMca){
                return R.success(ResultCode.SUCCESS);
            }else{
                return R.fail(ResultCode.FAILURE);
            }
        } catch (Exception e) {
            return R.fail(ResultCode.FAILURE);
        }
    }

    @Log("修改快递信息")
    @PutMapping
    // @RequiresPermissions("express:update")
    @ApiOperation(value = "修改快递信息",notes = "修改快递信息,传入mcaFile信息")
    public R updateMca(@Valid McaFile mcaFile){
        try {
            Boolean updateMca = this.iMcaFileService.updateMca(mcaFile);
            if(updateMca){
                return R.success(ResultCode.SUCCESS);
            }else{
                return R.fail(ResultCode.FAILURE);
            }
        } catch (Exception e) {
            return R.fail(ResultCode.FAILURE);
        }

    }

    @Log("删除快递信息")
    @DeleteMapping("/{mcaIds}")
    // @RequiresPermissions("express:delete")
    @ApiOperation(value = "删除快递信息",notes = "批量删除快递信息,传入要删除的快递信息id串,以逗号分隔")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "id串,以逗号分割",name = "mcaIds",required = true,paramType = "path")
    })
    public R deleteMca(@NotBlank(message = "{required}") @PathVariable String mcaIds){
        String[] ids = mcaIds.split(StringPool.COMMA);
        try {
            Boolean deleteMca = this.iMcaFileService.deleteMca(ids);
            if(deleteMca){
                return R.success(ResultCode.SUCCESS);
            }else{
                return R.fail(ResultCode.FAILURE);
            }
        } catch (Exception e) {
            return R.fail(ResultCode.FAILURE);
        }
    }

    @GetMapping("seekFiles")
    @ApiOperation(value = "查询指定快递",notes = "根据id查询唯一快递信息，传入快递信息的id")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "快递信息id",name = "id",required = true,paramType = "query")
    })
    public R<McaFile> seekFiles(@NotBlank(message = "{required}") String id) {
        McaFile mcaFile = this.iMcaFileService.seekMca(id);
        if(mcaFile != null){
            return R.data(mcaFile);
        }else{
            return R.fail(ResultCode.NO_CONTENT);
        }
    }
}